//
//  USMemorialDayWidget.swift
//  UsWidgetExtension
//
//  Created by karry on 2025/6/13.
//

import WidgetKit
import SwiftUI

struct USMemorialDayEntry: TimelineEntry {
    let date: Date
    let title: String
    let model: USMemorialDayModel
}

struct USMemorialDayProvider: AppIntentTimelineProvider {
    typealias ConfigurationIntent = USMemorialDaySelectionIntent

    func placeholder(in context: Context) -> USMemorialDayEntry {
        USMemorialDayEntry(date: Date(), title: "我们的纪念日",model: USMemorialDayModel.defaultValue)
    }

    func snapshot(for configuration: USMemorialDaySelectionIntent, in context: Context) async -> USMemorialDayEntry {
        USMemorialDayEntry(date: Date(), title: configuration.anniversary!.name, model: configuration.anniversary!)
    }

    func timeline(for configuration: USMemorialDaySelectionIntent, in context: Context) async -> Timeline<USMemorialDayEntry> {
        let entry = USMemorialDayEntry(date: Date(), title: configuration.anniversary!.name, model: configuration.anniversary!)
        return Timeline(entries: [entry], policy: .never)
    }
}


struct USMemorialDayWidgetEntryView: View {
    var entry: USMemorialDayEntry
    
    var tintColor:Color{
        Color(hex: entry.model.color) ?? .red
    }

    var body: some View {
        
        
        VStack{
            ZStack{
                tintColor.opacity(0.5)
                Text(entry.model.name)
                    .foregroundColor(tintColor.darken(0.35))
                    .font(.system(size: 17,weight: .medium))
                    .minimumScaleFactor(0.5)
                
            }.frame(height: 44)
            .padding(0)
            
            if entry.model.repeatYearly{
                
                HStack(alignment: .firstTextBaseline, spacing: 2) {
                    Text("还有")
                        .font(.system(size: 15))
                        .foregroundColor(.black)
                    Text("\(entry.model.dayCount())")
                        .font(.system(size: 28, weight: .medium, design: .rounded))
                        .foregroundColor(Color.green)
                    Text("天")
                        .font(.system(size: 15))
                        .foregroundColor(.black)
                }.frame(maxHeight: .infinity)
                
            }else{
                
                VStack(spacing: 4) {
                    
                    Text("Day+")
                        .font(.caption)
                        .foregroundColor(.gray)
                    
                    HStack(alignment: .firstTextBaseline, spacing: 2) {
                        Text("\(entry.model.dayCount())")
                            .font(.system(size: 28, weight: .medium, design: .rounded))
                            .foregroundColor(tintColor)
                        Text("天")
                            .font(.system(size: 15))
                            .foregroundColor(.black)
                    }
                    
                }.frame(maxHeight: .infinity)
                
            }
            
  
            
            Text(entry.model.dayDescription())
                .font(.footnote)
                .foregroundColor(.gray)
                .frame(height: 30)
            
        }.padding(0)
        
    }
}

struct USMemorialDayWidget: Widget {
    let kind: String = "USMemorialDayWidget"

    var body: some WidgetConfiguration {
        AppIntentConfiguration(kind: kind, intent: USMemorialDaySelectionIntent.self, provider: USMemorialDayProvider()) { entry in
            USMemorialDayWidgetEntryView(entry: entry)
                .containerBackground(Color("usBackgroundColor"), for: .widget)
                .padding(0)
        }
        .contentMarginsDisabled()
        .configurationDisplayName("纪念日小组件")
        .description("展示你选择的纪念日")
        .supportedFamilies([.systemSmall])
    }
}
